package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.result.BaseContext;
import com.itheima.reggie.result.R;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /*
    * 查询购物车数据
    * */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                .orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(lqw);
        return R.success(list);
    }


    /*
     * 添加购物车数据
     * */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, currentId);
        //获取菜品id，判断是否为菜品
        Long dishId = shoppingCart.getDishId();
        if (dishId != null) {
            //是菜品
            lqw.eq(ShoppingCart::getDishId, dishId);
        } else {
            //是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        //判断购物车中有无当前菜品或套餐
        ShoppingCart cartOne = shoppingCartService.getOne(lqw);
        if (cartOne != null) {
            //购物车有当前菜品或套餐，数量加1
            cartOne.setNumber(cartOne.getNumber() + 1);
            shoppingCartService.updateById(cartOne);
        } else {
            //购物车没有当前菜品或套餐，数量默认1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartOne = shoppingCart;
        }

        return R.success(cartOne);
    }


    /*
     * 清空购物车
     * */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        shoppingCartService.remove(lqw);

        return R.success("清空成功！");
    }

    /*
     * 减号减少数量
     * */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, userId);

        //判断是菜品还是套餐
        if (shoppingCart.getDishId() != null) {
            //为菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //为套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart cartServiceOne = shoppingCartService.getOne(lqw);

        //数量减1
        cartServiceOne.setNumber(cartServiceOne.getNumber() - 1);
        shoppingCartService.updateById(cartServiceOne);
        if (cartServiceOne.getNumber() == 0) {
            shoppingCartService.removeById(cartServiceOne.getId());
        }

        return R.success(cartServiceOne);
    }
}
